package net.sppan.base.controller.warehouse;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import net.sppan.base.common.JsonResult;
import net.sppan.base.common.PageBean;
import net.sppan.base.controller.BaseController;
import net.sppan.base.entity.Department;
import net.sppan.base.entity.Employee;
import net.sppan.base.entity.Equipment;
import net.sppan.base.service.DeparmentService;
import net.sppan.base.service.EmployeeService;
import net.sppan.base.service.EquipmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Controller
@RequestMapping("/warehouse")
public class WarehouseController extends BaseController{
    @Autowired
    private DeparmentService deparmentService;
	@Autowired
	private EquipmentService equipmentService;
    /**
     * @Description:仪器页面
     * @Param:
     * @return:
     */
	@RequestMapping(value ="/equipmentIndex")
	public String equipmentIndex(){
		return "warehouse/equipmentIndex";
	}

    /**
     * @Description:仪器出借页面
     * @Param:
     * @return:
     */
    @RequestMapping(value ="/equipmentRentIndex")
    public String equipmentRentIndex(){
        return "warehouse/equipmentRentIndex";
    }

    /**
     * @Description:获取仪器列表数据
     * @Param:
     * @return:
     */
    @RequestMapping(value = { "/getEquipmentList" })
	@ResponseBody
	public PageBean getequipmentList(@RequestParam("pageSize") Integer pageSize,@RequestParam("pageNumber")Integer pageNumber,
                                    @RequestParam(value = "searchText", required = false)String searchText,@RequestParam(value ="sortOrder",required = false)String sortOrder) {
		Page<Equipment> page = PageHelper.startPage(pageNumber, pageSize, true);//分页插件
        List<Equipment> resultList = equipmentService.getEquipmentList(searchText);
		PageBean<Equipment> pageBean = new PageBean<>(page.getPageNum(), page.getPageSize(), (int) page.getTotal());
        pageBean.setContent(resultList);
		return pageBean;
	}

    /**
     * @Description:添加框
     * @Param:
     * @return:
     */
    @RequestMapping(value = "/addEquipmentForm", method = RequestMethod.GET)
    public String addEquipmentForm(ModelMap map) {
        return "warehouse/addEquipmentForm";
    }

    /**
     * @Description:添加仪器
     * @Param:
     * @return:
     */
    @RequestMapping(value= {"/addEquipment"} ,method = RequestMethod.POST)
    @ResponseBody
    public JsonResult edit(Equipment equipment, ModelMap map){
        try {
            if(equipment.getDeviceId() == null ){
                return JsonResult.failure("缺少参数");
            }
            equipmentService.addEquipment(equipment);
        } catch (Exception e) {
            return JsonResult.failure(e.getMessage());
        }
        return JsonResult.success();
    }
    /** 
     * @Description: 获取剩下没入库的设备
     * @Param:  
     * @return:  
     */
    @RequestMapping(value = "/getLessDevice",method = RequestMethod.POST)
    @ResponseBody
    public JsonResult getAllDepartments(){
        JsonResult jsonResult = JsonResult.success();
        List<Equipment> equipmentList = equipmentService.getLessDevice();
        jsonResult.setData(equipmentList);
        return jsonResult;
    }
    @RequestMapping(value = "/rentForm/{id}", method = RequestMethod.GET)
    public String rentForm(@PathVariable Integer id, ModelMap map) {
        map.put("id", id);
        return "warehouse/rentForm";
    }
    @RequestMapping(value = "/getEmployeeList",method = RequestMethod.POST)
    @ResponseBody
    public JsonResult getEmployeeList(){
        JsonResult jsonResult = JsonResult.success();
        List<Employee> employeeList = equipmentService.getEmployeeList();
        jsonResult.setData(employeeList);
        return jsonResult;
    }
    /**
     * @Description: 出借仪器给到具体职工
     * @Param:  
     * @return:  
     */
    @RequestMapping(value= {"/rent"} ,method = RequestMethod.POST)
    @ResponseBody
    public JsonResult rent(Equipment equipment, ModelMap map){
        try {
            equipmentService.rent(equipment);
        } catch (Exception e) {
            return JsonResult.failure(e.getMessage());
        }
        return JsonResult.success();
    }
    /**
     * @Description:分页查询已经出借的设备
     * @Param:
     * @return:
     */
    @RequestMapping(value = { "/getRentEquipmentList" })
    @ResponseBody
    public PageBean getRentEquipmentList(@RequestParam("pageSize") Integer pageSize,@RequestParam("pageNumber")Integer pageNumber,
                                     @RequestParam(value = "searchText", required = false)String searchText,@RequestParam(value ="sortOrder",required = false)String sortOrder) {
        Page<Equipment> page = PageHelper.startPage(pageNumber, pageSize, true);
        List<Equipment> resultList = equipmentService.getRentEquipmentList(searchText);
        PageBean<Equipment> pageBean = new PageBean<>(page.getPageNum(), page.getPageSize(), (int) page.getTotal());
        pageBean.setContent(resultList);
        return pageBean;
    }
    /** 
     * @Description: 归还仪器
     * @Param:  
     * @return:  
     */
    @RequestMapping(value= {"/returnEquipment"} ,method = RequestMethod.POST)
    @ResponseBody
    public JsonResult returnEquipment(Equipment equipment, ModelMap map){
        try {
            equipmentService.returnEquipment(equipment);
        } catch (Exception e) {
            return JsonResult.failure(e.getMessage());
        }
        return JsonResult.success();
    }
}
